package com.chen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
public class NewsController {
    @Autowired
    RedisTemplate redisTemplate;



    //浏览量

    @RequestMapping("/detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        //阅读数加一
        Long readNumber = redisTemplate.opsForHash().increment("new_" + id, "read", 1);
        //查询点赞数
        Map map = redisTemplate.opsForHash().entries("new_" + id);
        model.addAllAttributes(map);
        //如果我们id传的是1
        return "new" + id;   //你这里跳的就是new1；  你看看有没new11页面没有啊现在就可以了不行啊
    }

    //点赞功能
    @RequestMapping("/addZan/{id}/{opType}")
    @ResponseBody
    public Long addZan(@PathVariable Integer id, @PathVariable Integer opType) {
        Long countZan;
        if (opType == 1) {//点赞
            countZan = redisTemplate.opsForHash().increment("news_" + id, "countZan", 1);
        } else {
            //取消点赞
            countZan = redisTemplate.opsForHash().increment("news_" + id, "countZan", -1);
        }
        return countZan;
    }
}
